package com.wendy.admin.controller;

import com.wendy.admin.domain.bo.SysLogininforBo;
import com.wendy.admin.domain.vo.SysLogininforExcelVo;
import com.wendy.admin.domain.vo.SysLogininforVo;
import com.wendy.admin.entity.SysLogininfor;
import com.wendy.admin.service.ISysLogininforService;
import com.wendy.cache.redis.service.RedisService;
import com.wendy.common.core.constant.CacheConstants;
import com.wendy.common.core.domain.TableDataInfo;
import com.wendy.common.log.annotation.Log;
import com.wendy.common.log.enums.BusinessType;
import com.wendy.common.web.controller.BaseController;
import com.wendy.custom.security.annotation.InnerAuth;
import com.wendy.custom.security.annotation.RequiresPermissions;
import com.wendy.excel.ruoyi.utils.ExcelUtil;
import com.wendy.utils.spring.MapstructUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 系统访问记录
 * 
 * @author wendy
 */
@RestController
@RequestMapping("/logininfor")
public class SysLoginInfoController extends BaseController
{
    @Autowired
    private ISysLogininforService logininforService;

    @Autowired
    private RedisService redisService;

    @RequiresPermissions("system:logininfor:list")
    @GetMapping("/page")
    public TableDataInfo page(SysLogininforBo logininfor) {
        TableDataInfo<SysLogininforVo> result = logininforService.selectLogininforPage(logininfor);
        return result;
    }

    @Log(title = "登录日志", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:logininfor:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysLogininforBo logininfor)
    {
        List<SysLogininfor> list = logininforService.selectLogininforList(logininfor);
        List<SysLogininforExcelVo> rlist = MapstructUtils.convert(list, SysLogininforExcelVo.class);
       ExcelUtil<SysLogininforExcelVo> util = new ExcelUtil<SysLogininforExcelVo>(SysLogininforExcelVo.class);
        util.exportExcel(response, rlist, "登录日志");
    }

    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/{infoIds}")
    public int remove(@PathVariable Long[] infoIds)
    {
        return logininforService.deleteLogininforByIds(infoIds);
    }

    @RequiresPermissions("system:logininfor:remove")
    @Log(title = "登录日志", businessType = BusinessType.DELETE)
    @DeleteMapping("/clean")
    public boolean clean()
    {
        logininforService.cleanLogininfor();
        return true;
    }

    @RequiresPermissions("system:logininfor:unlock")
    @Log(title = "账户解锁", businessType = BusinessType.OTHER)
    @GetMapping("/unlock/{userName}")
    public boolean unlock(@PathVariable("userName") String userName)
    {
        redisService.deleteObject(CacheConstants.PWD_ERR_CNT_KEY + userName);
        return true;
    }

    @InnerAuth
    @PostMapping
    public int add(@RequestBody SysLogininfor logininfor)
    {
        return logininforService.insertLogininfor(logininfor);
    }
}
